package com.mrd.gtimpframe.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.mrd.gtimpframe.entity.TGenSupSrvProvide;
import com.mrd.gtimpframe.repository.TGenSupSrvProvideMapper;
import com.mrd.gtimpframe.service.TGenSupSrvProvideService;
import com.mrd.util.KeyGenerator;

@Service("tGenSupSrvProvideService")
public class TGenSupSrvProvideServiceImpl implements TGenSupSrvProvideService {
  @Autowired
  private TGenSupSrvProvideMapper tGenSupSrvProvideMapper;

  @Override
  @Transactional
  public int deleteByPrimaryKey(Object id) {
    return tGenSupSrvProvideMapper.deleteByPrimaryKey(id);
  }

  @Override
  @Transactional
  public int insert(TGenSupSrvProvide t) {
    if (StringUtils.isEmpty(t.getId())) {
      t.setId(KeyGenerator.generateRadomSequenceNo("supSrv"));
    }
    return tGenSupSrvProvideMapper.insert(t);
  }

  @Override
  @Transactional
  public int insertSelective(TGenSupSrvProvide t) {
    if (StringUtils.isEmpty(t.getId())) {
      t.setId(KeyGenerator.generateRadomSequenceNo("supSrv"));
    }
    return tGenSupSrvProvideMapper.insertSelective(t);
  }

  @Override
  public TGenSupSrvProvide selectByPrimaryKey(Object id) {
    return tGenSupSrvProvideMapper.selectByPrimaryKey(id);
  }

  @Override
  @Transactional
  public int updateByPrimaryKeySelective(TGenSupSrvProvide t) {
    return tGenSupSrvProvideMapper.updateByPrimaryKeySelective(t);
  }

  @Override
  @Transactional
  public int updateByPrimaryKey(TGenSupSrvProvide t) {
    return tGenSupSrvProvideMapper.updateByPrimaryKey(t);
  }

  /**
   * 根据对象查询所有
   * 
   * @author han
   * @return
   */
  @Override
  public List<TGenSupSrvProvide> queryList(TGenSupSrvProvide tGenSupSrvProvide) {
    return tGenSupSrvProvideMapper.queryList(tGenSupSrvProvide);
  }

  @Transactional
  public int deleteBatch(Object[] id) {
    return tGenSupSrvProvideMapper.deleteBatch(id);
  }

  /**
   * 查询线下供应商
   * 
   * @author han
   * @return
   */
  public List<JSONObject> selOffLineCompany(String code) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("code", code);
    List<Map<String, Object>> list = tGenSupSrvProvideMapper.selOffLineCompany(map);
    List<JSONObject> rtnList = new ArrayList<JSONObject>();
    if (list != null && list.size() > 0) {
      for (Map<String, Object> item : list) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", String.valueOf(item.get("COM_ID")));
        jsonObject.put("name", String.valueOf(item.get("COM_NAME")));
        jsonObject.put("port", String.valueOf(item.get("PORT")));
        jsonObject.put("port_code", String.valueOf(item.get("PORT_CODE")));
        jsonObject.put("com_customs_code", String.valueOf(item.get("COM_CUSTOMS_CODE")));
        jsonObject.put("hasSO", String.valueOf(item.get("HAS_SO")));
        rtnList.add(jsonObject);
      }
    }
    return rtnList;
  }

  /**
   * 查询口岸下的供应商
   * 
   * @author han
   * @return
   */
  public List<JSONObject> selectOffLineSup(String code) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("code", code);
    List<JSONObject> rtnList = new ArrayList<JSONObject>();
    List<Map<String, Object>> list = tGenSupSrvProvideMapper.selectOffLineSup(map);
    if (list != null && list.size() > 0) {
      for (Map<String, Object> item : list) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", String.valueOf(item.get("COM_ID")));
        jsonObject.put("name", String.valueOf(item.get("COM_NAME")));
        rtnList.add(jsonObject);
      }
    }
    return rtnList;
  }

  /**
   * 查询报关行或者进出口
   * 
   * @author han
   * @param code
   * @return
   */
  public List<JSONObject> selCompanyGkBgx(String code) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("code", code);
    List<Map<String, Object>> list = tGenSupSrvProvideMapper.selectOffLineSup(map);
    List<JSONObject> rtnList = new ArrayList<JSONObject>();
    if (list != null && list.size() > 0) {
      for (Map<String, Object> item : list) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", String.valueOf(item.get("COM_ID")));
        jsonObject.put("name", String.valueOf(item.get("COM_NAME")));
        jsonObject.put("port", String.valueOf(item.get("PORT")));
        jsonObject.put("port_code", String.valueOf(item.get("PORT_CODE")));
        jsonObject.put("com_customs_code", String.valueOf(item.get("COM_CUSTOMS_CODE")));
        rtnList.add(jsonObject);
      }
    }
    return rtnList;
  }
}
